<?php

namespace plugins\fund\controller;

use cmf\controller\ApiPluginBaseController; //引用插件基类
use plugins\fund\common\Common;
use plugins\pay\lib\Pay;
use think\Db;
use think\Validate;
use plugins\fund\model\FundAccountModel;

class ApiIndexController extends ApiPluginBaseController
{

    private function getUid()
    {
        $getIdentity = $this->getIdentity();
        $uid = $getIdentity->uid;
        return $uid;
    }

    /**
     * 用户流水列表
     */
    public function recordList()
    {
        $data     = $this->request->post();
        $validate = new Validate([
            'pageNum' => 'require',
            'page'    => 'require',
            'type'    => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid   = $this->getUid();
        $where = 1;
        $where .= " and uid='" . $uid . "'";
        switch ($data['type']) {
            case 'all':
                break;
            case 1:
                $where .= " and type=1";
                break;
            case 2:
                $where .= " and type=2";
                break;
            case 3:
                $where .= " and type=3";
                break;
            default:
                return api_json_echo(false, '类型错误', null, 100);
                break;
        }
        $list = Db::name('fund')->where($where)->paginate($data['pageNum'], '', ['page' => $data['page']]);
        foreach ($list as $key => $value) {
            $value['add_time'] = date('Y-m-d H:i:s');

            $list[$key] = $value;
        }
        return api_json_echo(true, '获取成功', $list, 200);
    }

    /**
     * 添加账户
     */
    public function addAccount()
    {
        $data     = $this->request->post();
        $validate = new Validate([
            'name'         => 'require',
            'phone'        => 'require|number|max:11|regex:/^1[3-8]{1}[0-9]{9}$/',
            'account'      => 'require',
            'account_type' => 'require',
            'is_first'     => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();

        $member = Db::name('member')->where('uid', $uid)->find();
        if (empty($member)) {
            return api_json_echo(false, '用户不存在', null, -2);
        }

        $pay_list = Common::getPayList();
        $pay_list = array_column($pay_list, 'icon', 'type');

        $add = [
            'name'         => $data['name'],
            'phone'        => $data['phone'],
            'account'      => $data['account'],
            'account_type' => $data['account_type'],
            'remark'       => !empty($data['remark']) ? $data['remark'] : '',
            'uid'          => $uid,
            'addtime'      => time(),
            'is_first'     => $data['is_first'],
            'nickname'     => $member['nick_name'],
            'icon'         => !empty($pay_list[$data['account_type']]) ? $pay_list[$data['account_type']] : '',
        ];

        $FundAccountModel = new FundAccountModel();

        if ($data['is_first'] == 1) {
            //先把其他的改为2
            $FundAccountModel->where("uid",$uid)->update(['is_first' => 2]);
        }

        $FundAccountModel->save($add);

        return api_json_echo(true, '绑定成功', null, 200);
    }

    /**
     * 账户信息
     */
    public function accountList()
    {
        $data = $this->request->post();
        $uid = $this->getUid();

        $pay_list = Common::getPayList();
        $pay_list = array_column($pay_list, 'name', 'type');

        $list = Db::name('fund_account')
            ->field('uid,del',true)
            ->where('uid', $uid)->select();

        foreach ($list as $key => $value) {
            if (!empty($pay_list[$value['account_type']])) {
                $value['account_type'] = $pay_list[$value['account_type']];
            }
            $value['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
            $list[$key] = $value;
        }
        return api_json_echo(true, '获取成功', $list, 200);
    }

    /**
     * 修改账户
     */
    public function editAccount()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'id' => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();

        $pay_list = Common::getPayList();
        $pay_list = array_column($pay_list, 'icon', 'type');

        $data['icon'] = !empty($pay_list[$data['account_type']]) ? $pay_list[$data['account_type']] : '';

        $FundAccountModel = new FundAccountModel();

        if (!empty($data['is_first']) && $data['is_first'] == 1) {
            //先把其他的改为2
            $FundAccountModel->where("uid",$uid)->update(['is_first' => 2]);
        }
        $where = "uid = {$uid} and id = ".$data['id'];
        $upd = $FundAccountModel->save($data,$where);

        return api_json_echo(true, '修改成功', null, 200);
    }

    /**
     * 设为默认
     */
    public function setDefault()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'id' => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();

        $FundAccountModel = new FundAccountModel();
        //先把其他的改为2
        $FundAccountModel->where("uid",$uid)->update(['is_first' => 2]);

        $data['is_first'] = 1;
        $where = "uid = {$uid} and id = ".$data['id'];
        $upd = $FundAccountModel->save($data,$where);
        return api_json_echo(true, '操作成功', null, 200);
    }

    /**
     * 充值
     */
    public function recharge()
    {
        $data     = $this->request->post();
        $validate = new Validate([
            'money'    => 'require',
            'pay_type' => 'require',
        ]);
        if (!$validate->check($data)) {
            return api_json_echo(false, $validate->getError(), null, -1);
        }
        $uid = $this->getUid();
        if ($data['pay_type'] == 'alipay') {
            $da = [
                'uid'         => $uid,
                'type'        => 3,
                'pay_type'    => 'alipay',
                'money'       => $data['money'],
                'body'        => '充值',
                'subject'     => '您有一个充值支付',
                'goods_type'  => 0,
                'time_expire' => date('Y-m-d H:i', time() + 900),
                'other'       => 'recharge' . date('ymdhi') . cmsrandom(10, 'all'),
            ];
            $record = new \plugins\fund\api_module\Record();
            $res = $record->pay($da);
            if ($res['status'] != 'success') {
                return api_json_echo(false, $res['message'], null, -1);
            }
            return api_json_echo(true, '成功', $res['data'], 200);
        }
    }

    /**

     * 提现

     */

    public function cashMoney()
    {

        //提现需要绑定账户

    }

}
